#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: tangzhichao

from pathlib import Path
from typing import Dict, Type, List
from .extractors.base import ContentExtractor, UnsupportedFormatError
from .extractors import (
    ExcelExtractor,
    DocxExtractor,
    MarkdownExtractor,
    TxtExtractor
)


class ExtractorFactory:
    """文档提取器工厂"""

    # 支持的格式映射表
    _extractor_map: Dict[str, Type[ContentExtractor]] = {
        '.xlsx': ExcelExtractor,
        '.xls': ExcelExtractor,
        '.docx': DocxExtractor,
        '.md': MarkdownExtractor,
        '.txt': TxtExtractor
    }

    @classmethod
    def get_extractor(cls, file_path: str) -> ContentExtractor:
        """获取适合文件类型的提取器实例"""
        ext = Path(file_path).suffix.lower()

        if ext not in cls._extractor_map:
            raise UnsupportedFormatError(ext)

        return cls._extractor_map[ext]()

    @classmethod
    def supported_formats(cls) -> List[str]:
        """获取支持的文件格式列表"""
        return list(cls._extractor_map.keys())